package com.manbearpig.todue;

import com.manbearpig.todue.User.invalidField;
import android.content.Context;
import android.widget.Toast;

/**
 * The Login_Manager class manipulates information
 * entered at the login screen and passes it to a
 * helper class to validate it.
 * @author Brandon Devereaux
 * @version 1.0
 */
public class Login_Manager {
	private String username;
	private String password;
	private String name;
	private String email;
	
	/**
	 * This method will be called when the user presses the login button on
	 * the login screen.
	 * 
	 * An error message will be displayed if the login is invalid
	 * @param c the context of the Android. Needed in case an error message needs to be shown.
	 */
	public boolean attemptLogin(Context c)
	{
		User potential = new User(this.username, this.password);
		if(potential.validateLogin(c))
		{
			this.popupError("Login Successful!",c);
			return true;
		}
		else
			this.popupError("Sorry, one or more fields are invalid.",c);
		return false;
	}
	
	/**
	 * This method passes a valid username to a new Todo_Manager
	 * @param username The valid username of the user whose ToDo's we will retrieve
	 */
	/*private void login(String username)
	{
		ToDo_Manager tdm = new ToDo_Manager();
		//tdm.retrieveToDos(username);
	}*/
	
	/**
	 * Raises a Toast displaying the reason for the failed login
	 * @param errorMessage The text to display in the Toast
	 * @param c the Context of the Android so an error message can be shown to the users
	 */
	private void popupError(String errorMessage, Context c)
	{
		Toast toast = Toast.makeText(c,errorMessage,Toast.LENGTH_LONG);
		toast.show();
	}
	
	/**
	 * This method passes information to a helper class which will attempt to create a new
	 * user profile.
	 * @param c The context to pass to the DatabaseHelper that we ultimately interface with
	 */
	public boolean attemptCreate(Context c)
	{
		User candidate = new User(name, email, username, password);
		invalidField validCredentials = candidate.validateAll(c);
		switch(validCredentials)
		{
			case NONE:
				candidate.storeUserCredentials(c);
				popupError("Account successfully created!",c);
				return true;
			case USERNAME:
				popupError("That username is already taken.",c);
				break;
			case EMAIL:
				popupError("Please enter a valid email address.",c);
				break;
		}
		return false;
		
	}
	
	
	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}
}
